package subraSoft.fenix.model.authentication.service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

public final class FenEncryptUtil
{
	private static FenEncryptUtil instance;
	private static Logger logger = Logger.getLogger(FenEncryptUtil.class);

	private FenEncryptUtil()
	{}
	
	public synchronized String encrypt(String text)
	{
		MessageDigest md = null;
		try
		{
			md = MessageDigest.getInstance("SHA");
		}
		catch(NoSuchAlgorithmException e)
		{
			logger.error(e,e);
			throw new RuntimeException(e);
		}
		try
		{
			md.update(text.getBytes("UTF-8"));
		}
		catch(UnsupportedEncodingException e)
		{
			logger.error(e,e);
			throw new RuntimeException(e);
		}
		byte raw[] = md.digest();
		String hash = (new BASE64Encoder()).encode(raw);
		return hash;
	}

	public static synchronized FenEncryptUtil getInstance() 
	{
		if(instance == null)
			instance = new FenEncryptUtil();
		return instance;
	}
}